#!/usr/bin/env python
#-*- coding: utf8 -*-
import ssl
import urllib.parse
import urllib.request
import json
import zlib
import urllib.error
import http.cookiejar

#
ssl._create_default_https_context = ssl._create_unverified_context
#
url = 'http://www.citivelocity.com/CompositePageService/mobile/mobile/CitVelEquitiesMob'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36",
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
       'Accept-Encoding': 'gzip, deflate, br',
       'Accept-Language': 'en-US,en;q=0.9',
       'Connection': 'keep-alive',
           'Content-Type': 'application/json;charset=utf-8',
           'Authority': 'www.citivelocity.com',
           'Set-Cookie': "SMSESSION'=r9LrTOB+o1BOIrr6fLPk99vBRIEzedUWeky7HaslhNPy56uCxhV1arKhjOAAgeNoAz7RQ5eNHMUhg/QU23DFcr7LmY0O0Se58nzTUWVBE9uHwqzIvLd+b3MBeAV6wMKfArMagpgCo9/0sFUPvSN+F099t3DYk6kvN66NQghn3l2X2IzEGEmw0Mbe3m1rPdCUlE5K3U+u0w498ijBjoPhVEvoD7ZberzIiBEN0qcNEnr8Iv+f9lNmvaWMCb4djPHTMyThSmo19SWNCgdckE7pdzwQCiX14EZ1B8DfYjxfcPpbT3+0RNd+zZOvjA/WdJRdF8IDgDKjpID0GRBHnzL+5wHi2d7XUCO+HNYYpnXMGD/AHFThjMVgdGE1atdLw2g6ELq0p2kQI2XwNcB9zLQ7NDt/i2fdvYHfyqm5XbX8VdzUg+LbwQg4huSywAXxJbx9KX+ip11buqqfmM9Nthln5JxDuipCB6zgYSuR/T8xWHNK8V3d65YQHP9+9stdlhD2luPQdjMr9K+xxF2+MM3Vjdwz0aMyPcFOwecRg/+SpljZfrL1u6D3g+sFWK25w6IaMBghj0EeHP2/7U4r+wB3wqGmPX00aYSYQjQR+Ilkz4dBABPQKWwFN5KonDpyII+vJu1m61Ids1CPTc4va2TBDRAy3wsEvTJ0f9PdS9v75H6ohr1ScqcR8r+eHOO+zqS1ofULvWqSQx50dOtyHdZEfXitQC6mI7FYWuVCx/KoTV/tpn/LVC0k224FbzXv0Q0tVH+OfQmrVKhr1r0uiOQrqHuKMy6+/5j7fmFWg/VLGdQvvne6V9tSOKry3h2/iczmBLzrhGA4P9a7bxLb3oAr91jUBgkVRXVWoRkCDjF48py9PhfdJxwlR/GlpEKd9R/X0PbsaqrS8XLF1QBO1yAH5Oz6BIU9RMULR9y5fXe8aGraZURV5AQdotjquLY1etv+OE23Ix/Ch2Ll3eLtxT9qNbIrGxEe3eeTf0CzH4PTCBW6tlJqiQ9AuD4sfYuxYFxml5RV7aFD4Z+I2BBRUj0RAK2cZTvcQEpr0A7ob/Yw3DLO/M0vw6iqcKlMc0WTS8jXUXJl+ivVIdWdGz6yc1qtbcN7kFHvwNAYIXMnuaiCnssckdK5VMCELkSVOD0glMHJ; path=/; domain=.citivelocity.com; secure"
           }

username = 'jl33389'
password = 'Open1sys@'
data={
    'username':username,
    'password':password,
}
postdata=urllib.parse.urlencode(data).encode('utf8')

# page = urllib2.urlopen(url).read()
# print(page)
urlData = "http://vocab.nic.in/rest.php/states/json"
def getResponse(url):

    # p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    # p.add_password(None, url, username, password)
    # handler = urllib.request.HTTPBasicAuthHandler(p)
    # opener = urllib.request.build_opener(handler)
    # urllib.request.install_opener(opener)

    operUrl = urllib.request.Request(url, headers=headers)
    # 使用http.cookiejar.CookieJar()创建CookieJar对象
    # cjar = http.cookiejar.CookieJar()

    # 使用HTTPCookieProcessor创建cookie处理器，并以其为参数构建opener对象
    # cookie = urllib.request.HTTPCookieProcessor(cjar)
    # opener = urllib.request.build_opener(cookie)
    # 将opener安装为全局
    # urllib.request.install_opener(opener)

    res = urllib.request.urlopen(operUrl)
    if(res.getcode()==200):
       data = res.read()
       jsonData = json.loads(data)
    else:
       print("Error receiving data", res.getcode())
    return jsonData

def main(url):
    jsonData = getResponse(url)
    # print the state id and state name corresponding
    for i in jsonData["states"]:
        print(f'State Name:  {i["state"]["state_name"]} , State ID : {i["state"]["state_id"]}')
if __name__ == '__main__':
    main(url)




